package org.ourgrid.worker.collectors.gmond;

import java.io.IOException;

import org.junit.Test;

import static org.junit.Assert.*;

public class GMondUtilsTest  {
	
	@Test
	public void testIsGMondInstalled() {
		assertTrue( GMondUtils.getInstance().isGMondInstalled() );
	}
	
	@Test
	public void testStartGMond() {
		
		if( GMondUtils.getInstance().isGMondInstalled() ) {		
			try {
				if(! GMondUtils.getInstance().isGMondRunning() ) {
					GMondUtils.getInstance().startGMond();
					Thread.sleep(2*1000);
					assertTrue(GMondUtils.getInstance().isGMondRunning());
				} else {
					GMondUtils.getInstance().stopGMond();
					Thread.sleep(2*1000);
					GMondUtils.getInstance().startGMond();
					Thread.sleep(2*1000);
					assertTrue(GMondUtils.getInstance().isGMondRunning());
				}
			} catch (IOException e) {
				fail();
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		} else {
			fail();
		}
		
	}
	
	@Test
	public void testStopGMond() {
		if( GMondUtils.getInstance().isGMondInstalled() ) {
			try {
				if(! GMondUtils.getInstance().isGMondRunning() ) {
					GMondUtils.getInstance().startGMond();
					Thread.sleep(2*1000);
					GMondUtils.getInstance().stopGMond();
					Thread.sleep(2*1000);
					assertTrue(! GMondUtils.getInstance().isGMondRunning());
				} else {
					GMondUtils.getInstance().stopGMond();
					Thread.sleep(2*1000);
					assertTrue(! GMondUtils.getInstance().isGMondRunning());
				}
			} catch (IOException e) {
				fail();
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		} else {
			fail();
		}
	}
	
	@Test
	public void testIsGMondRunning() {
		if( GMondUtils.getInstance().isGMondInstalled() ) {
			try {
				if(! GMondUtils.getInstance().isGMondRunning() ) {
					GMondUtils.getInstance().startGMond();
					Thread.sleep(2*1000);
					assertTrue(GMondUtils.getInstance().isGMondRunning());
					GMondUtils.getInstance().stopGMond();
					Thread.sleep(2*1000);
					assertTrue(! GMondUtils.getInstance().isGMondRunning());
				} else {
					assertTrue(GMondUtils.getInstance().isGMondRunning());
					GMondUtils.getInstance().stopGMond();
					Thread.sleep(2*1000);
					assertTrue(!GMondUtils.getInstance().isGMondRunning());
				}
			} catch (IOException e) {
				fail();
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		} else {
			fail();
		}
	}
	
}
